package com.bs.blogspace.service.impl;

import com.bs.blogspace.mapper.SpaceMapper;
import com.bs.blogspace.mapper.UserMapper;
import com.bs.blogspace.model.Result;
import com.bs.blogspace.pojo.Space;
import com.bs.blogspace.pojo.User;
import com.bs.blogspace.service.SpaceService;
import com.bs.blogspace.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class SpaceServiceImpl implements SpaceService {
    @Autowired
    private SpaceMapper spaceMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private UserMapper userMapper;

    @Override
    public Result<Space> createSpace(Space space) {
        Result<Space> result = new Result<>();
        space.setCreateTime(LocalDateTime.now());
        space.setUpdateTime(LocalDateTime.now());
        String text = (String) space.getSpaceBright().get("text");
        ArrayList<String> strings = new ArrayList<>();
        strings = (ArrayList<String>) space.getSpaceBright().get("image");
        String rs = "";
        for (int i = 0; i < strings.size(); i++) {
            String s = strings.get(i);
            rs = s + "@" +rs ;
        }
        space.setImage(rs);
        space.setText(text);
        space.setUserName(space.getSpaceName());
        User u = userMapper.findByPhone(space.getPhone());
        String username = u.getUserName();
        space.setUserName(username);
        spaceMapper.insert(space);
        result.setSuccess(true);
        result.setCode("200");
        result.setMessage("注册成功");
        result.setData(space);
        return result;
    }

    @Override
    public Result getAllSpace() {
        Result result = new Result();
        List<Space> spaceList = spaceMapper.getAll();
        if (spaceList == null){
            result.setCode("404");
            result.setMessage("没有已创建的空间");
            return result;
        }
        Map<String,Object> spaceBright = new HashMap<>();
        for (Space space : spaceList) {
            parseSpace(space,spaceBright);
        }
        result.setData(spaceList);
        result.setCode("200");
        result.setMessage("获取所有空间成功");
        result.setSuccess(true);
        return result;
    }

    @Override
    public Result getBySpace(Integer spaceId) {
        Result result = new Result();
        if (spaceId == null){
            result.setMessage("参数不能为空");
            result.setCode("404");
            return result;
        }
        Space space = spaceMapper.getBySpace(String.valueOf(spaceId));
        if (space == null){
            result.setCode("404");
            result.setMessage("该用户不存在");
            return result;
        }

        Map<String,Object> spaceBright = new HashMap<>();
        parseSpace(space,spaceBright);
        result.setData(space);
        result.setSuccess(true);
        result.setMessage("获取单个用户成功");
        result.setCode("200");

        return result;
    }

    @Override
    public Result queryUserSpace(String phone) {
        Result<Object> result = new Result<>();
        List<Space> spaceList = spaceMapper.queryUserSpace(phone);
        if (spaceList.size() == 0){
            result.setMessage("该用户还没有创建空间");
            result.setCode("414");
            return result;
        }
        ArrayList<Map<String, String>> list = new ArrayList<>();
        for (int i = 0; i < spaceList.size(); i++) {
            Map<String, String> map = new HashMap<>();
            map.put("spaceId", String.valueOf(spaceList.get(i).getSpaceId()));
            map.put("spaceName",spaceList.get(i).getSpaceName());
            list.add(map);
        }
        result.setData(list);
        result.setSuccess(true);
        result.setMessage("查询成功");
        result.setCode("200");
        return result;
    }


    /**
     *  从获取的space对象中解析出Controller中需要的数据类型.
     * @param space
     * @param spaceBright
     */
    private void parseSpace(Space space, Map<String, Object> spaceBright) {
        space.setUserName(userService.getByPhone(space.getPhone()).getUserName());
        LocalDateTime nowDateTime = LocalDateTime.now();
        Duration duration = Duration.between(space.getCreateTime(),nowDateTime);
        space.setCreateDay(duration.toDays() + 1);
        spaceBright.put("text",space.getText());
        spaceBright.put("image",space.getImage().split("@"));
        space.setSpaceBright(spaceBright);
    }

}
